<?php

namespace IDock\Insurance\ZSIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZSIC\Response\AllZSICResponse;
use IDock\Insurance\ZSIC\ZSICEnums;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;


class VehicleModelResponse extends AllZSICResponse
{

    public function printerInit(): Response
    {
        $vehicleModelPrinter = new VehicleModelPrinter();
        $modelList = [];
        $body = $this->getBody()['body'] ?? [];
        $modelList = $this->formatArray($body['vehicles']['vehicle'] ?? []);

        foreach ($modelList as $datum) {
            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['modelCode'] ?? '';
            $vehiclePrinter->item['platformModelCode'] = $datum['platformModelCode'] ?? '';
            $vehiclePrinter->item['brand'] = $datum['brand'] ?? '';
            $vehiclePrinter->brandName = $datum['brand'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['capacity'] ?? '';

            $vehiclePrinter->vehicleName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehiclePriceTax = $datum['purchasePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['seat'] ?? '';
            $vehiclePrinter->vehicleImport =  array_search($datum['importFlag'],ZSICEnums::$importFlag) ?: 'I1';//vehicleImport
            $vehiclePrinter->power = $datum['power'] ?? '';

            $vehiclePrinter->vehicleTonnage = $datum['tonnage'] ?? '';
            $vehiclePrinter->wholeWeight = $datum['weight'] ?? '';
            $typeArr = array_flip(ZSICEnums::$energyType);
            unset($typeArr['']);
            $vehiclePrinter->energyTypeCode = $typeArr[$datum['fuelType'] ?? ''] ?? 'E01';
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode] ?? '';
            //$vehiclePrinter->item = $datum;
            $energyType = $datum['fuelType'] ?? '';
            if ($energyType == '1' or $energyType == '3') {
                $vehiclePrinter->isNewEnergy = '1';
            }
            $vehicleModelPrinter->setModelList($vehiclePrinter);
        }


        $this->printer = $vehicleModelPrinter;

        return $this;
    }

    public function formatArray($arr)
    {
        if(empty($arr)) return [];
        if(!isset($arr[0])){
            $tmp = $arr;
            unset($arr);
            $arr[] = $tmp;
        }
        return $arr;
    }
}
